/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.pacs.model;

import com.cxhd.pacs.DataConfig;
import com.cxhd.pacs.tools.DateUtil;
import com.cxhd.pacs.tools.MainTool;
import com.google.gson.Gson;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Administrator
 */
public class HostDetail extends Model<HostDetail> {

    Gson gson = new Gson();

    public List<HostDetail> getDetail(String host_id) {
        //  System.out.println(find("select * from host_detail"));
        //return find("select * from host_detail where host_id = '" + host_id + "' and `status`=0 order by event_time desc");
        return find("select a.*,c.`name`as hospital_n from host_detail a LEFT JOIN hostinfo b USING(host_id) LEFT JOIN hospital c on b.hospital_code=c.hospital_code where a.host_id = '" + host_id + "' and a.`status`=0 order by a.event_time desc");
        //return gson.toJson(find("select * from host_detail where host_id='"+host_id+"'"));
    }

    public String getResult(String pk) {
       
        return findFirst("select * from host_detail where pk=" + pk).get("event_result");
    }

    public String saveDetail(Controller con) {

        try {
            String event_time = con.getPara("event_time");
            if (con.getPara("event_time") == null) {

                event_time = DateUtil.getDateEN();

            }
            set("host_id", con.getPara("host_id"))
                    .set("department", con.getPara("department"))
                    .set("event_name", con.getPara("event_name"))
                    .set("event_description", con.getPara("event_description"))
                    .set("event_result", con.getPara("event_result"))
                    .set("event_time", event_time)
                    .set("hospital_name",DataConfig.getInstance().getHospitalName() )
                    .set("createtime", DateUtil.getDateEN())
                    .set("result_type", con.getPara("result_type"))
                    .set("uuid", con.getPara("uuid"))
                    .set("bed_no", con.getPara("bed_no"))
                    .set("result_status", con.getPara("result_status"))
                    .save();

            Db.update("update hostinfo set last_event_name ='" + con.getPara("event_name") + "',last_event_time='" + event_time + "' where host_id='" + con.getPara("host_id") + "'");
            return get("pk")+"";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    public void saveDetail(Map<String, String> con) throws Exception {

        set("host_id", con.get("host_id"))
                .set("department", con.get("department"))
                .set("event_name", con.get("event_name"))
                .set("event_description", con.get("event_description"))
                .set("event_result", con.get("event_result"))
                .set("event_time", DateUtil.getDateEN())
                .set("hospital_name", DataConfig.getInstance().getHospitalName())
                .set("createtime", DateUtil.getDateEN())
                .set("result_type", con.get("result_type"))
                .set("data_type", con.get("data_type"))
                .set("update_id", con.get("update_id"))
                .set("bed_no", con.get("bed_no"))
                .set("data1", con.get("data1"))
                .save();

        Db.update("update hostinfo set last_event_name ='" + con.get("event_name") + "',last_event_time='" + DateUtil.getDateEN() + "' where host_id='" + con.get("host_id") + "'");

    }

    public String saveDetail2(Map<String, String> con) throws Exception{
        String detail_id = null;
            
            set("host_id", con.get("host_id"))
                    .set("department", con.get("department"))
                    .set("event_name", con.get("event_name"))
                    .set("event_description", con.get("event_description"))
                    .set("event_result", con.get("event_result"))
                    .set("event_time", con.get("event_time") == null ? DateUtil.getDateEN() : con.get("event_time"))
                    .set("hospital_code",con.get("hospital_code") )
                    .set("info_id",con.get("info_id") )
                    .set("createtime", DateUtil.getDateEN())
                    .set("doctor", con.get("department"))
                    .set("result_type", con.get("result_type"))
                    .set("data_type", con.get("data_type"))
                    .set("update_id", con.get("update_id"))
                    .set("bed_no", con.get("bed_no"))
                    .save();
            detail_id = get("pk") + "";
            
            System.out.println("update hostinfo set bed_no='" + con.get("bed_no") + "', last_event_name ='" + con.get("event_name") + "',last_event_time='" + con.get("event_time")+ "' where host_id='" + con.get("host_id") + "'");
            Db.update("update hostinfo set bed_no='" + con.get("bed_no") + "', last_event_name ='" + con.get("event_name") + "',last_event_time='" + con.get("event_time")+ "' where host_id='" + con.get("host_id") + "'");
       

        return detail_id;
    }

    public String updateDetail(Map<String, String> con) {
       // String detail_id = null;
        try {
            System.out.println("" + con);
           
            Db.update("update host_detail set department=?,event_name=?,event_description=?,event_result=?,result_type=?,data_type=?,bed_no=? ,event_time=? where update_id=? and host_id=?",
                    con.get("department"), con.get("event_name"), con.get("event_description"), con.get("event_result"), con.get("result_type"), con.get("data_type"), con.get("bed_no"), con.get("event_time"),con.get("update_id"),con.get("host_id"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Db.findFirst("select * from host_detail where update_id=? and host_id=?", con.get("update_id"),con.get("host_id")).get("pk")+"";
    }

    public boolean checkUpdateId(String update_id,String hostId) {
        if (update_id == null) {
            return true;
        }
        return findFirst("select * from host_detail where update_id=? and host_id=?", update_id,hostId) == null;

    }

    public String deleteItem(String pk) {
        try {
            findById(pk).set("status", "1").update();
            return "true";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "false";
    }

    public void pasteDetail(String pk, String host_id) {

        findById(pk).set("host_id", host_id).update();
    }

}
